/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn;

import java.util.Scanner;
import java.util.StringJoiner;

/**
 * <p>Project: app2022 - Ex2
 * <p>Powered by webrx On 2022-01-13 10:18:07
 *
* @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex2 {
    //static  String [] strArray={"abc","bca","acd","cab","123","231","123123"};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StringJoiner stringJoiner = new StringJoiner(",");
        System.out.println("输入很多字符串，空格隔开，以字符串#结束");
        while(!scanner.hasNext("#")){
            stringJoiner.add(scanner.next());
        }
        String [] strArray=stringJoiner.toString().split(",");
        StringBuilder stringBuilder = new StringBuilder();
        int [] flag=new int[strArray.length];
        for (int i = 0; i < strArray.length-1;) {
            if(strArray[i]==null){
                i++;
                continue;
            }
            int width=strArray[i].length();
            stringBuilder.append(strArray[i]);
            int x=0;
            for(int j=i+1;j< strArray.length&&strArray[j]!=null;j++){
                int sum=0;
                for(int k=0;k<strArray[j].length();k++){
                    if(strArray[i].contains(""+strArray[j].charAt(k))){
                        sum++;
                    }
                    else{
                        sum=0;
                        break;
                    }
                }
                if(sum>=strArray[i].length()){
                    width+=strArray[j].length()+1;
                    stringBuilder.append(","+strArray[j]);
                    strArray[j]=null;
                    x++;
                }

            }
            if(x>0) {
                flag[i] = width+1;
                stringBuilder.append(",");
                //i+=x+1;
                for(int j=1;j<strArray.length;){
                    if(strArray[j]==null){
                        j++;
                        continue;
                    }else{
                        i+=j;
                        break;
                    }
                }
            }
            else{
                flag[i]=width+1;
                stringBuilder.append(",");
                i++;
            }
        }
        String s = stringBuilder.toString();
        System.out.println(s);
        int index=0;
        System.out.print("[");
        for(int i=0;i<flag.length;i++){
            strArray = s.substring(index, index + flag[i]).split(",");
            index+=flag[i];
            if(flag[i]!=0) {
                //System.out.print(Arrays.toString(strArray));
                System.out.print("[");
                for(int j=0;j< strArray.length;j++){
                    if(j==0){
                        System.out.print("\""+strArray[j]+"\"");
                    }
                    else{
                        System.out.print(","+"\""+strArray[j]+"\"");
                    }
                }
                System.out.print("]");
            }
        }
        System.out.println("]");
    }
}
